package org.opens.javaskill.jvm.classloader.thread;

import org.junit.Test;
import org.opens.javaskill.jvm.classloader.CustomizeClassLoader;

import java.sql.Driver;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.ServiceLoader;

/**
 * @Date 2020/10/31 17:48
 * @Created by 99126
 */
public class ThreadClassLoader {

    public static void main(String[] args) {
        ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
        System.out.println(contextClassLoader);

        Thread.currentThread().setContextClassLoader(new CustomizeClassLoader());
        System.out.println(Thread.currentThread().getContextClassLoader());

    }

    /**
     * 功能:
     * <p>验证rt.jar的内容由哪个加载器加载</p>
     * 输出:
     * <pre>
     *  null
     * </pre>
     * 说明:
     * <pre>
     *      1. 输出为null的话就是boot加载器进行加载的.
     * </pre>
     */
    @Test
    public void test33() throws ClassNotFoundException {
        Class<?> aClass = Class.forName("java.sql.Driver");
        // rt.jar由根加载器加载
        System.out.println(aClass.getClassLoader());
    }

    /**
     * 功能:
     * <p></p>
     * 输出:
     * <pre>
     *
     * </pre>
     * 说明:
     * <pre>
     *      1.
     * </pre>
     */
    @Test
    public void test53() throws ClassNotFoundException, SQLException {
        Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
        System.out.println(aClass.getClassLoader());
        System.out.println(Driver.class.getClassLoader());
        System.out.println(com.mysql.jdbc.Driver.class.getClassLoader());
    }

    /**
     * 功能:
     * <p></p>
     * 输出:
     * <pre>
     *
     * </pre>
     * 说明:
     * <pre>
     *      1.
     * </pre>
     */
    @Test
    public void test76() throws ClassNotFoundException {
        Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
        ServiceLoader<Driver> load = ServiceLoader.load(Driver.class);
        Iterator<Driver> driversIterator = load.iterator();
        while(driversIterator.hasNext()) {
            driversIterator.next();
        }
        System.out.println(Driver.class.getClassLoader());
    }

    /**
     * 功能:
     * <p></p>
     * 输出:
     * <pre>
     *
     * </pre>
     * 说明:
     * <pre>
     *     1.
     * </pre>
     */
    @Test
    public void test101() {

    }

}
